#!/usr/bin/perl
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use POSIX 'strftime';
my $readin;
read(STDIN, $readin, $ENV{'CONTENT_LENGTH'});
my $inputstring = $readin;
my $schar = substr($inputstring, 1, 1);
	print "Content-type: text/html\n\n";
	my %hash = ();
	my ($pairs,$key,$value);
	my @pairsarr = split(/&/, $inputstring);
	foreach my $pairs (@pairsarr){
		my @keyvalue = split(/=/, $pairs);
		$key = $keyvalue[0];
		$value = $keyvalue[1];
		$hash{ $key } = $value;
	}
	my $time = $hash{"time"};
	my $doc = $hash{"doc"};
	my $action = $hash{"action"};
	my $user = $hash{"user"};
	my $docvalue = $hash{"docvalue"};
	
	open (FILE,"appointment.html");
	while (<FILE>) {
		print $_;
	}
	print '<INPUT TYPE="text" name="username" id="username" value="'.$user.'"\>';
	print '<INPUT TYPE="text" name="docvalueb" id="docvalueb" value="'.$docvalue.'"\>';
	close(FILE);
	my $sep = '+';
	my @docnames= split /\Q$sep\E/, $doc;
	my $date = strftime "%Y%m%d", localtime;
	while($time > 8){
		$date = $date+1;
		$time = $time - 8;
	}
	$time = $time + 8;
	$date = $date + 1;
	my $actiontag = "rd";
	if($action eq "cancel"){
		$actiontag = "c";
	}
	#basically, this just takes POST and edits the reservations. Most of this is just parsing
	my $command = "/usr/bin/perl ./edit_reserves.pl -$actiontag '$docnames[0] $docnames[1]' $user $date $time ";
	system($command);
	system("/usr/bin/perl ./schedule.pl $user");

	
